package com.carkeeper.subject.service.impl;

import com.carkeeper.common.utils.IdWorker;
import com.carkeeper.common.utils.QiniuUtils;
import com.carkeeper.common.utils.RedisConstant;
import com.carkeeper.subject.dao.StoreDetailMapper;
import com.carkeeper.subject.dao.StoreMapper;
import com.carkeeper.subject.pojo.Merchant;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.pojo.StoreDetail;
import com.carkeeper.subject.pojo.StorePoi;
import com.carkeeper.subject.service.StoreService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private StoreDetailMapper storeDetailMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /*
    * 根据id查询Store
    * */
    @Override
    public Store findById(Long id) {
        if (id == null){
            throw new RuntimeException("id不存在");
        }
        Store store = storeMapper.selectByPrimaryKey(id);
        return store;
    }

    /*
    * 多条件查询
    * */
    @Override
    public List<Store> findBySearch(Store store) {
        //自定义类
        Example example = new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();

        //汽修厂简称
        if (store.getSimpleName() !=null && !"".equals(store.getSimpleName())){
            criteria.andLike("simpleName","%"+store.getSimpleName()+"%");
        }
        //汽修厂编码
        if (store.getCode() !=null && !"".equals(store.getCode())){
            criteria.andLike("code","%"+store.getCode()+"%");
        }
        //城市 省
        if (store.getProvince() !=null && !"".equals(store.getProvince())){
            criteria.andEqualTo("province",store.getProvince());
        }
        //城市 市
        if (store.getCity() !=null && !"".equals(store.getCity())){
            criteria.andEqualTo("city",store.getCity());
        }
        //城市 区
        if (store.getCounty() !=null && !"".equals(store.getCounty())){
            criteria.andEqualTo("county",store.getCounty());
        }
        //联系电话
        if (store.getContactPhone() !=null && !"".equals(store.getContactPhone())){
            criteria.andEqualTo("contactPhone",store.getContactPhone());
        }
        //是否连锁
        if (store.getIsChain() !=null && !"".equals(store.getIsChain())){
            criteria.andEqualTo("isChain",store.getIsChain());
        }
        //状态
        if (store.getStatus() !=null && !"".equals(store.getStatus())){
            criteria.andEqualTo("status",store.getStatus());
        }

        //执行查询接口,自定义查询
        List<Store> stores = storeMapper.selectByExample(example);
        return stores;
    }
    /*
    * 分页
    * */
    @Override
    public Page<Store> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Page<Store> pageList = (Page<Store>) storeMapper.selectAll();
        return pageList;
    }
    /*
    * 新增服务商
    * */
    @Override
    public void add(Merchant merchant) {
        if (merchant == null){
            throw new RuntimeException("数据不存在");
        }
        Store store = merchant.getStore();
        //雪花算法生成id
        long id = idWorker.nextId();
        store.setId(id);
        //生成6位编码
        Integer code = generateValidateCode(6);
        store.setCode(String.valueOf(code));
        store.setStatus("1");           //设置锁定  0锁定  1正常
        store.setAuditStatus("0");      //设置待审核 0待审核 1审核成功 2审核拒绝
        store.setCreateTime(new Date());  //设置创建时间
        storeMapper.insertSelective(store);
        //商家详情表
        StoreDetail storeDetail = merchant.getStoreDetail();
        storeDetail.setId(store.getId());
        stringRedisTemplate.boundSetOps(RedisConstant.PIC_DB_RESOURCES).add(storeDetail.getBusinessLicenseUrl());
        stringRedisTemplate.boundSetOps(RedisConstant.PIC_DB_RESOURCES).add(storeDetail.getCorporationObverseUrl());
        stringRedisTemplate.boundSetOps(RedisConstant.PIC_DB_RESOURCES).add(storeDetail.getCorporationBackUrl());

        storeDetail.setBusinessLicenseUrl(QiniuUtils.qiNiuYunUrl+"/"+storeDetail.getBusinessLicenseUrl());
        storeDetail.setCorporationObverseUrl(QiniuUtils.qiNiuYunUrl+"/"+storeDetail.getCorporationObverseUrl());
        storeDetail.setCorporationBackUrl(QiniuUtils.qiNiuYunUrl+"/"+storeDetail.getCorporationBackUrl());

        storeDetailMapper.insertSelective(storeDetail);
    }

    /*
    * 随机生成6位数字编码
    * */
    public Integer generateValidateCode(int length){
        Integer code =null;
        if(length == 6){
            code = new Random().nextInt(999999);//生成随机数，最大为999999
            if(code < 100000){
                code = code + 100000;//保证随机数为6位数字
            }
        }else{
            throw new RuntimeException("只能生成6位数字");
        }
        return code;
    }

    /*
     * 锁定服务商
     * */
    @Override
    public void lock(Long id) {
        if (id == null ){
            throw new RuntimeException("服务商不存在");
        }
        Store store = storeMapper.selectByPrimaryKey(id);
        //设置状态为0 锁定
        store.setStatus("0");
        //更改服务商数据
        storeMapper.updateByPrimaryKeySelective(store);
    }

    /*
     * 解锁服务商
     * */
    @Override
    public void unlock(Long id){
        if (id == null ){
            throw new RuntimeException("服务商不存在");
        }
        Store store = storeMapper.selectByPrimaryKey(id);
        //设置状态为1  解锁
        store.setStatus("1");
        //更改服务商数据
        storeMapper.updateByPrimaryKeySelective(store);
    }
    /*
    * 根据服务商id查询查询商家 和商家详情
    * */
    @Override
    public Merchant findByStoreId(Long id) {
        if (id == null){
            throw new RuntimeException("服务商不存在");
        }
        Merchant merchant = new Merchant();
        //服务商
        Store store = storeMapper.selectByPrimaryKey(id);
        merchant.setStore(store);
        //商家详情
        StoreDetail storeDetail = new StoreDetail();
        //商家详情添加商家的id
        storeDetail.setId(store.getId());
        //根据商家id查询商家详情
        storeDetail = storeDetailMapper.selectOne(storeDetail);
        //封装数据
        merchant.setStoreDetail(storeDetail);
        return merchant;
    }

    /*
    * 修改服务商主体
    * */
    @Override
    public void update(Merchant merchant) {
        if (merchant == null){
            throw new RuntimeException("数据不存在");
        }
        Store store = merchant.getStore();

        //修改服务商表数据
        storeMapper.updateByPrimaryKeySelective(store);
        StoreDetail storeDetail = merchant.getStoreDetail();
        //商家详情设置id
        storeDetail.setId(store.getId());
        //修改服务商详情数据
        storeDetailMapper.updateByPrimaryKeySelective(storeDetail);
    }

    /*
     * 根据tb_order表的storeID查询store表
     * */
    @Override
    public Store findByOrderStoreId(Long id) {
        if (id == null){
            throw new RuntimeException("数据不存在");
        }
        Store store = new Store();
        store.setId(id);
        store = storeMapper.selectOne(store);
        return store;
    }

    /*
     * 查询storePoi表所有数据
     * */
    @Override
    public List<StorePoi> findAll() {
        //查询所有store表数据
        List<Store> storeList = storeMapper.selectAll();
        //创一个集合
        List<StorePoi> storeList1 = new ArrayList<>();
        if (storeList != null && storeList.size() > 0) {
            Integer count = 1;
            //变量store表数据把数据存入storePoi中
            for (Store store : storeList) {
                StorePoi storeOne = new StorePoi();
                storeOne.setId(count++);        //序号
                storeOne.setCode(store.getCode());  //编码
                storeOne.setSimpleName(store.getSimpleName());  //简称
                storeOne.setContactName(store.getContactName());    //联系人
                storeOne.setContactPhone(store.getContactPhone());  //联系电话
                if ("0".equals(store.getIsChain())){
                    store.setIsChain("否");
                }
                if ("1".equals(store.getIsChain())){
                    store.setIsChain("是");
                }
                storeOne.setIsChain(store.getIsChain());        //是否连锁 0否 1是
                storeOne.setProvince(store.getProvince()+store.getCity()+store.getCounty()); //所在区域
                if ("0".equals(store.getStatus())){
                    store.setStatus("锁定");
                }
                if ("1".equals(store.getStatus())){
                    store.setStatus("正常");
                }
                storeOne.setStatus(store.getStatus()); //是否锁定
                storeList1.add(storeOne);
            }
        }
        return storeList1;
    }


}
